package com.zhiyou100.crm.servlet.customerSource;

import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.pojo.AdminBaseServlet;
import com.zhiyou100.crm.pojo.CustomerSource;
import com.zhiyou100.crm.pojo.User;
import com.zhiyou100.crm.service.CustomerSourceService;
import com.zhiyou100.crm.service.impl.CustomerSourceServiceImpl;
/**
 * 更新角色
 */
@WebServlet("/customerSource/update")
public class UpdateServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	CustomerSourceService customerSourceService = new CustomerSourceServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		int customerSourceId = Integer.parseInt(request.getParameter("id"));
		List<CustomerSource> customerSources = customerSourceService.qureyAll();
		for (int i = 0; i < customerSources.size(); i++) {
			if(customerSources.get(i).getCustomer_source_id()==customerSourceId){
				request.setAttribute("customerSource",customerSources.get(i));
				break;
			}
		}
		request.getRequestDispatcher("/WEB-INF/view/customerSource/update.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String customerSourceName = request.getParameter("customerSourceName");
		String customerSourceDesc = request.getParameter("customerSourceDesc");
		int customerSourceId = Integer.parseInt(request.getParameter("customerSourceId"));
		User user = (User) request.getSession().getAttribute("user");
		Timestamp time= Timestamp.valueOf(LocalDateTime.now());
		request.getRequestDispatcher("/WEB-INF/view/customerSource/update.jsp").forward(request, response);
		CustomerSource customerSource=new CustomerSource(customerSourceId,customerSourceName,customerSourceDesc,time,user.getUser_id());
		int update = customerSourceService.update(customerSource);
		if(update>0) {
			response.sendRedirect(request.getContextPath() + "/customerCategory/list");
		}else {
			response.sendRedirect(request.getHeader("referer"));
		}
	}

}
